//
//  SISSHouseResourceDetailVC.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/24.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHouseResourceDetailVC.h"
#import "SISSPropertyCertificateViewController.h"
#import "SISSNavigationController.h"
#import "SISSRecommendPassengerViewController.h"
#import "SISSMorePopupView.h"
#import "SISSPhotoBrowser.h"

@interface SISSHouseResourceDetailVC ()<SISSMorePopupViewDelegate,SDCycleScrollViewDelegate,MWPhotoBrowserDelegate>

@property (weak, nonatomic) IBOutlet SDCycleScrollView *cycleScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cycleScrollViewConstraint;


@property (weak, nonatomic) IBOutlet UILabel *neighbourhoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndDetailLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelsViewHeightConstraint;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *listTagLabels;
@property (weak, nonatomic) IBOutlet UILabel *propertyStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *averagePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *directionLabel;
@property (weak, nonatomic) IBOutlet UILabel *yearLabel;
@property (weak, nonatomic) IBOutlet UILabel *decorationLabel;
@property (weak, nonatomic) IBOutlet UILabel *floorLabel;
@property (weak, nonatomic) IBOutlet UILabel *housecodingLabel;
@property (weak, nonatomic) IBOutlet UILabel *insertTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherDescriptionLabel;

@property (weak, nonatomic) IBOutlet UIView *propertyCoverView;
@property (weak, nonatomic) IBOutlet UIImageView *jumpImageView;

@property (weak, nonatomic) IBOutlet UIButton *recommendButton;

@property (strong, nonatomic) UIView *backView;


@property (weak, nonatomic) UILabel *pageLabel;
/* 图片链接数组 */
@property (strong, nonatomic) NSArray *imagesURLStrings;

@end

@implementation SISSHouseResourceDetailVC



- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setCycleScrollViewAndPageLabel];
    
    for (UILabel *label in self.listTagLabels) {
        
        label.hidden = YES;
//        self.labelsViewHeight.constant = 1;
    }
    
    self.recommendButton.hidden = self.isHideBottomButton;
    
    // 添加右侧分享投诉按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)];
}

//- (void)viewWillAppear:(BOOL)animated{
//    
//    [super viewWillAppear:animated];
//    
//    //    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//    //设置导航栏背景图片为一个空的image，这样就透明了
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    //去掉透明后导航栏下边的黑边
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//    
//    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
//}
//
//- (void)viewWillDisappear:(BOOL)animated{
//    
//    [super viewWillDisappear:animated];
//    
//    //    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
//    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
//    //    如果不想让其他页面的导航栏变为透明 需要重置
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:nil];
//}


#pragma mark -- SDCycleScrollViewDelegate 点击图片看大图
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [photoBrowser setCurrentPhotoIndex:index];
//    [photoBrowser showNextPhotoAnimated:YES];
//    [photoBrowser showPreviousPhotoAnimated:YES];
    SISSNavigationController *navigationController = [[SISSNavigationController alloc] initWithRootViewController:photoBrowser];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.imagesURLStrings.count;
}

- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    
    //创建图片模型
    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.imagesURLStrings[index]]];
    return photo;
}



- (void)setHouseResourceModel:(SISSHouseResourceModel *)houseResourceModel {
    
    [self.view layoutIfNeeded];
    _houseResourceModel = houseResourceModel;
    
    if (houseResourceModel.houseImage.count > 0) {
        self.imagesURLStrings = houseResourceModel.houseImage;
        self.pageLabel.text = [NSString stringWithFormat:@"%d/%ld",1,self.imagesURLStrings.count];
        self.cycleScrollView.imageURLStringsGroup = houseResourceModel.houseImage;
        self.cycleScrollViewConstraint.constant = 160;
    } else {
        self.cycleScrollView.hidden = !houseResourceModel.houseImage;
        self.cycleScrollViewConstraint.constant = 0.01;
        self.pageLabel.hidden = YES;
    }
    
    if (houseResourceModel.listTag.count > 0) {
        self.labelsViewHeightConstraint.constant = 20;
    } else {
        self.labelsViewHeightConstraint.constant = 1;
    }
    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    
    for (int i = 0; i < houseResourceModel.listTag.count; i++) {
        UILabel *label = self.listTagLabels[i];
        label.hidden = NO;
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@" %@ ",houseResourceModel.listTag[i].tag];
        NSInteger colorIndex = houseResourceModel.listTag[i].color - 1;
        label.layer.borderColor = labelCololors[colorIndex].CGColor;
        label.textColor = labelCololors[colorIndex];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    self.neighbourhoodLabel.text = houseResourceModel.neighbourhood;
    self.regionAndDetailLabel.text = [NSString stringWithFormat:@"%@-%@",houseResourceModel.region,houseResourceModel.regionDetail];
    
    self.priceLabel.text = [NSString stringWithFormat:@"%0.f万",[houseResourceModel.price floatValue]];
    self.houseTypeLabel.text = houseResourceModel.houseType;
    
    self.areaLabel.text = [NSString stringWithFormat:@"%@㎡",houseResourceModel.area];
    
    if ([houseResourceModel.propertyState isEqualToString:@"共有产权"]) {
        self.jumpImageView.hidden = NO;
        self.propertyCoverView.hidden = NO;
    } else {
        self.jumpImageView.hidden = YES;
        self.propertyCoverView.hidden = YES;
    }
   
    self.propertyStateLabel.text = houseResourceModel.propertyState;
    self.averagePriceLabel.text = [NSString stringWithFormat:@"%.0f元/㎡",[houseResourceModel.averagePrice floatValue] * 10000];
    self.directionLabel.text = houseResourceModel.direction ? houseResourceModel.direction : @"暂无数据";
    self.yearLabel.text = houseResourceModel.year ? [NSString stringWithFormat:@"%@年",houseResourceModel.year] : @"暂无数据";
    self.decorationLabel.text = houseResourceModel.decoration.length ? houseResourceModel.decoration : @"暂无数据";
    
    NSArray *floorsArr = @[@"1-10层", @"11-20层", @"21-30层", @"30层以上"];
    self.floorLabel.text = houseResourceModel.floor ? floorsArr[houseResourceModel.floor/11] : @"暂无数据";
    self.housecodingLabel.text = houseResourceModel.houseInfoNo;
    self.otherDescriptionLabel.text = houseResourceModel.otherdesc.length ? houseResourceModel.otherdesc : @"暂无数据";
    self.insertTimeLabel.text = [NSDate yyr_stringToString:houseResourceModel.insertTime];
}


/**
 设置图片轮播器相关
 */
- (void)setCycleScrollViewAndPageLabel {
    
    // 设置图片查看
    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"图片加载占位符"];
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.autoScroll = NO;
    self.cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    self.cycleScrollView.itemDidScrollOperationBlock = ^(NSInteger currentIndex) {
        
        self.pageLabel.text = [NSString stringWithFormat:@"%zd/%zd",currentIndex + 1,self.imagesURLStrings.count];
    };
    
    UILabel *pageLabel = [[UILabel alloc] init];
    self.pageLabel = pageLabel;
    self.pageLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
    self.pageLabel.textColor = [UIColor whiteColor];
    self.pageLabel.text = [NSString stringWithFormat:@" %d/%ld ",1,self.imagesURLStrings.count];
    self.pageLabel.textAlignment = NSTextAlignmentCenter;
    self.pageLabel.layer.cornerRadius = 10;
    self.pageLabel.layer.masksToBounds = YES;
    [self.cycleScrollView addSubview:self.pageLabel];
    
    CGFloat margin = 8.0;
    WeakSelf
    [self.pageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.cycleScrollView).offset(-2 * margin);
        make.width.mas_equalTo(5 * margin);
        make.bottom.equalTo(weakSelf.cycleScrollView).offset(-margin);
    }];
}

/**
 产权证明调整
 */
- (IBAction)propertyStateGestureAction:(UITapGestureRecognizer *)sender {
    
    SISSPropertyCertificateViewController *vc = [[SISSPropertyCertificateViewController alloc] init];
    SISSNavigationController *nav = [[SISSNavigationController alloc] initWithRootViewController:vc];
    vc.title = @"产权证明";
    vc.propertyFileArr = self.houseResourceModel.lstOfFile;
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}


/**
 推荐客源按钮点击事件
 */
- (IBAction)recommendButtonClickAction:(UIButton *)sender {
    
    SISSRecommendPassengerViewController *vc = [[SISSRecommendPassengerViewController alloc] init];
    vc.ahrId = self.houseResourceModel.ID;
    vc.isFind = self.isFind;
    vc.isDetailIn = YES;
    vc.title = @"客源列表";
    [self.navigationController pushViewController:vc animated:YES];
}


/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:@[@"分享", @"举报"]];
    moreView.delegate = self;
    [moreView show];
}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (index == 0) {
        
        NSString *title = [NSString stringWithFormat:@"%@ | %@万",self.houseResourceModel.neighbourhood, self.houseResourceModel.price];
        NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",self.houseResourceModel.region,self.houseResourceModel.regionDetail,self.houseResourceModel.area,self.houseResourceModel.houseType];
        NSString *webpageUrl = @"";
        if (self.houseResourceModel.houseresType == 1) { // 线上
            webpageUrl = [NSString stringWithFormat:@"%@/?#!/sellerHouseDetail/%@",WEB_BASE_URL,self.houseResourceModel.ID];
        } else { // 线下
            webpageUrl = [NSString stringWithFormat:@"%@/#!/offlineHouseDetail/%@",WEB_BASE_URL,self.houseResourceModel.ID];
        }
        
        [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:self.houseResourceModel.houseImage.firstObject];
    } else {
        
        SISSReportViewController *vc = [[SISSReportViewController alloc] init];
        vc.houseModel = self.houseResourceModel;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}


@end
